package com.defect.controller.basics;

import com.defect.bo.ResultBo;
import com.defect.po.User;
import com.defect.service.securityService.LUserService;
import com.defect.util.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class BUserController {
    @Resource
    private LUserService userService;

    /**
     * 查询所有用户
     * @return
     * @throws Exception
     */
    @GetMapping
    public ResultBo selectUserAll(String name, Page page) throws Exception {
        System.out.println(page.getPageIndex());
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("pageIndex", page.getPageIndex());
        map.put("number", page.getNumber());
        long dataCount = userService.cont(map);
        page.setDataCount(dataCount);
        return new ResultBo("200","success",page,userService.selectUserAll(map));
    }

    /**
     * 新增用户
     * @return
     * @throws Exception
     */
    @PostMapping
    public ResultBo insertUser(User user) throws Exception {
        return new ResultBo("200","success",userService.insertUser(user),null);
    }

    /**
     * 修改用户
     * @return
     * @throws Exception
     */
    @RequestMapping("updata")
    public ResultBo updataUser(User user) throws Exception {
        return new ResultBo("200","success",userService.updataUser(user),null);
    }

    /**
     * 批量删除用户
     * @return
     * @throws Exception
     */
    @DeleteMapping
    public ResultBo deleteUsers(Integer[] arr) throws Exception {
        userService.delateUsers(arr);
        return new ResultBo("200","success",null,null);
    }

    /**
     * 删除用户
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    public ResultBo deleteUsers(Integer id) throws Exception {
        System.out.println(id);
        userService.delateUser(id);
        return new ResultBo("200","success",null,null);
    }
}
